<?php
/**
 * Pertyler
 * Object Browser's Row's Objects Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require '../library/Pert/General/Link.php';

require '../library/Pert/General/Page.php';

require '../databaseModel/Table.php';

require '../databaseModel/Role.php';

require '../control/main.php';

if(!\Pert\Page::Get()->HREF()->ParentTable)
	return false;

if(!\Pert\Page::Get()->HREF()->Row)
	return false;

if(!($moderator = Role_Moderator::Select()))
	if(require '../control/Login.php')
		return;

$parentTable = Table::Select(\Pert\Page::Get()->HREF()->ParentTable);

$row = $parentTable->SelectRow(\Pert\Page::Get()->HREF()->Row);

require 'KoolControls/KoolAjax/koolajax.php';

require 'KoolControls/KoolTreeView/kooltreeview.php';

$treeView = new \KoolTreeView('treeView');

$treeView->imageFolder = $iconsFolder;

$treeView->showLines = true;

foreach($row->SelectColumnValueArray() AS $columnValue)
{
	if(($columnValue->Value()))
	{
		if($columnValue->Column()->RelatedTable())
		{
			$link = new \Pert\Link('ObjectBrowser.php', 'body');

			$link->ParentTable = $columnValue->Column()->RelatedTable()->Id();

			$link->Row = $columnValue->Value()->Id();

			$hTML = '
<i >' . $columnValue->Column() . '</i>' . $link->HTML($columnValue->Value());

			$treeView->Add('root',
					\Pert\Page::Get()->HREF()->TreeNode . $columnValue->Column()->BackgroundName(),
					$hTML,
					false,
					'ball_glass_redS.gif');
		}
		elseif($columnValue->Column()->Priority() > 1)
		{
			if(($columnValue->Column()->ColumnType() == 'integer')
					OR ($columnValue->Column()->ColumnType() == 'string')
					OR ($columnValue->Column()->ColumnType() == 'date')
					OR ($columnValue->Column()->ColumnType() == 'eMail')
					OR ($columnValue->Column()->ColumnType() == 'webSide')
					OR ($columnValue->Column()->ColumnType() == 'float'))
			{
				$hTML = '
<i >' . $columnValue->Column() . '</i>
' . $columnValue->Value();

				$treeView->Add('root',
						\Pert\Page::Get()->HREF()->TreeNode . $columnValue->Column()->BackgroundName(),
						$hTML,
						false,
						'ball_glass_greenS.gif');
			}
			else
			{
				$hTML = '
<i ><u >' . $columnValue->Column() . '</u></i>';

				$treeView->Add('root',
						\Pert\Page::Get()->HREF()->TreeNode . $columnValue->Column()->BackgroundName(),
						$hTML,
						false,
						'ball_glass_yellowS.gif');
			}
		}
	}
}

foreach($parentTable->SelectRelatedArray() AS $relatedTable)
{
	$treeNodesLink = new \Pert\Link('ObjectBrowser_relatedTablesObjects.php');

	$treeNodesLink->ParentTable = $parentTable->Id();

	$treeNodesLink->ParentRow = $row->Id() ;

	$treeNodesLink->Table = $relatedTable->Id();

	$treeView->Add('root',
			$row->Id() . 'Related' . $relatedTable->UniqueName(),
			$relatedTable,
			false,
			'triangle_yellowS.gif',
			$treeNodesLink->Href());
}

if($row instanceof PublishedRow)
	foreach($row->SelectGroupColumnValueArray() AS $groupColumnValue)
	{
		$link = new \Pert\Link('group.php', 'body');

		$link->Table = $parentTable->Id();

		$link->Row = $row->Id();

		$link->Column = $groupColumnValue->Column()->Priority();

		if($groupColumnValue->Value())
		{
			$treeNodesLink = new \Pert\Link('ObjectBrowser_groupsObjects.php');

			$treeNodesLink->ParentTable = $parentTable->Id();

			$treeNodesLink->Row = $row->Id() ;

			$treeNodesLink->Column = $groupColumnValue->Column()->Priority();

			$treeView->Add('root',
					$row->Id() . $groupColumnValue->Column()->BackgroundName(),
					$link->HTML($groupColumnValue->Column()),
					false,
					'ball_glass_blueS.gif',
					$treeNodesLink->Href());
		}
		else
			$treeView->Add('root',
					$row->Id() . $groupColumnValue->Column()->BackgroundName(),
					$link->HTML($groupColumnValue->Column()),
					false,
					'ball_glass_blueS.gif');
	}

foreach($row->SelectTableArray() AS $table)
{
	$link = new \Pert\Link('RowUnderRowArray.php', 'body');

	$link->Row = $row->Id();

	$link->Table = $table->Id();

	if(file_exists($treeView->imageFolder . $table->Schema() . '.gif'))
		$icon = $table->Schema() . '.gif';
	else
		$icon = 'public.gif';

	$treeNodesLink = new \Pert\Link('ObjectBrowser_tablesObjects.php');

	$treeNodesLink->ParentTable = $parentTable->Id();

	$treeNodesLink->ParentRow = $row->Id() ;

	$treeNodesLink->Table = $table->Id();

	$treeView->Add('root',
			$row->Id() . $table->UniqueName(),
			$link->HTML($table),
			false,
			$icon,
			$treeNodesLink->Href());
}

$treeView->isSubTree = true;

$treeView->width = '250px';

echo $treeView->Render();
